package com.business.web.controller;

import com.business.activiti.model.auto.AppealInfo;
import com.business.activiti.service.ActivitiService;
import com.business.activiti.service.AppealInfoService;
import com.business.appealtype.model.auto.AppealType;
import com.business.appealtype.service.AppealTypeService;
import com.business.customer.model.auto.CustomerInfo;
import com.business.customer.service.CustomerInfoService;
import com.business.form.model.auto.FormFieldInfo;
import com.business.form.model.auto.FormFieldInfoExample;
import com.business.form.model.auto.FormInfo;
import com.business.form.service.FormFieldInfoService;
import com.business.form.service.FormInfoService;
import com.business.system.common.base.BaseController;
import com.business.system.common.domain.AjaxResult;
import com.business.system.model.auto.TsysUser;
import com.business.weixin.util.WeixinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

/**
 * 用户Controller
 * @ClassName: UserController
 * @author fuce
 * @date 2019-11-20 22:35
 */
@Api(value = "网络接口")
@RestController
@RequestMapping("/web")
public class WebController extends BaseController {

    @Autowired
    private ActivitiService activitiService;

    @Autowired
    private FormInfoService formInfoService;
    @Autowired
    private FormFieldInfoService formFieldInfoService;
    @Autowired
    private AppealTypeService appealTypeService;
    @Autowired
    private AppealInfoService appealInfoService;
    @Autowired
    private CustomerInfoService customerInfoService;
    @Autowired
    private WeixinService weixinService;

    @RequestMapping("/getStartFormFieldList")
    @ApiOperation(value = "根据模型key获取流程开始的表单和表单字段数据")
    public AjaxResult getFormFieldList(HttpServletRequest req){
        String processKey=req.getParameter("processKey");
        String formKey=activitiService.getProcessDefinitionStartElementFormKey(processKey);
        if(StringUtils.isNotBlank(formKey)){
            FormInfo formInfo=formInfoService.selectByPrimaryKey(formKey);
            FormFieldInfoExample fe=new FormFieldInfoExample();
            fe.createCriteria().andFormIdEqualTo(formKey);
            List<FormFieldInfo> formFieldList=formFieldInfoService.selectByExample(fe);
            return AjaxResult.success().put("formInfo",formInfo).put("formFieldList",formFieldList);
        }else{
            return AjaxResult.error();
        }
    }

    @RequestMapping("/getFormFieldListById")
    @ApiOperation(value = "根据表单定义id获取表单和表单字段数据")
    public AjaxResult getFormFieldListById(HttpServletRequest req){
        String formId=req.getParameter("formId");
        if(StringUtils.isNotBlank(formId)){
            FormInfo formInfo=formInfoService.selectByPrimaryKey(formId);
            FormFieldInfoExample fe=new FormFieldInfoExample();
            fe.createCriteria().andFormIdEqualTo(formId);
            List<FormFieldInfo> formFieldList=formFieldInfoService.selectByExample(fe);
            return AjaxResult.success().put("formInfo",formInfo).put("formFieldList",formFieldList);
        }else{
            return AjaxResult.error();
        }
    }

    @RequestMapping("/addAppealInstance")
    @ApiOperation(value = "增加一个诉求流程实例-启动一个流程含表单")
    public AjaxResult addAppealInstance(HttpServletRequest req){
        //所需参数 诉求平台来源 用户id 诉求类型  表单中的字段数据
        Enumeration enu=req.getParameterNames();
        HashMap<String,String> map=new HashMap<String,String>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        String openid=map.get("openid");//openid
        if(StringUtils.isBlank(openid)){
            return AjaxResult.error("未获取到微信用户信息");
        }
        HashMap<String,String> selectmap=new HashMap<String,String>();
        selectmap.put("openid",openid);
        List<CustomerInfo> customers=customerInfoService.selectByMap(selectmap);
        if(customers==null||customers.size()<=0){
            return AjaxResult.error("该微信用户未注册");
        }
        String appealSource =map.get("appealSource");//注册来源
        String appealTypeId=map.get("appealTypeId");//诉求类型id 绑定了流程定义
        if(StringUtils.isBlank(appealTypeId)){
            return AjaxResult.error("缺少诉求类型");
        }
        activitiService.startProcessAndAppeal(map,appealTypeId,customers.get(0).getUserId(),appealSource);
        return AjaxResult.success();
    }

    @CrossOrigin
    @RequestMapping("/getAppealTypeList")
    @ApiOperation(value = "获取所有诉求类型，供前端select渲染")
    public AjaxResult getAppealTypeList(){
        List<AppealType> appealTypes = appealTypeService.selectByExample(null);
        return AjaxResult.success().put("selectList",appealTypes);
    }
    @RequestMapping("/getAppealInfoList")
    @ApiOperation(value = "根据用户id获取诉求信息，用于前端 我的诉求")
    public AjaxResult getAppealInfoList(HttpServletRequest req){
        String appealInfoId = req.getParameter("appealInfoId");
        AppealInfo appealInfo = appealInfoService.selectByPrimaryKey(appealInfoId);
        return AjaxResult.success().put("appealInfo",appealInfo);
    }


    @RequestMapping("/initWechatUser")
    @ApiOperation(value = "初始化微信用户")
    public AjaxResult initWechatUser(HttpServletRequest req){
        Enumeration enu=req.getParameterNames();
        HashMap<String,String> map=new HashMap<String,String>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        CustomerInfo customerInfo=customerInfoService.initWechatCustomer(map);
        return AjaxResult.success().put("customerInfo",customerInfo);
    }

    @RequestMapping("/downloadWechatMedia")
    @ApiOperation(value = "mediaId")
    public AjaxResult downloadWechatMedia(HttpServletRequest req){
        String mediaId=req.getParameter("mediaId");
        weixinService.downloadMedia(mediaId);
        return AjaxResult.success();
    }

    @RequestMapping("/MP_verify_7y5T6j7nj69bFaAP.txt")
    @ApiOperation(value = "安全域名")
    public void MP_verify_7y5T6j7nj69bFaAP(HttpServletRequest req, HttpServletResponse rep) throws IOException {
        rep.getWriter().write("7y5T6j7nj69bFaAP");
    }

}
